package com.hkj.configcenter.core.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.common.IOUtils;

/**
 * 配置文件读取类
 */
@Slf4j
public class PropertiesUtils {

    /**
     * 将制定路径的配置文件读取到MAP对象中
     */
    public static Map<String, String> getPropsMap(String filePath) {
        InputStream is = null;
        try {
            is = PropertiesUtils.class.getResourceAsStream(filePath);
            if (is == null) {
                throw new IllegalArgumentException(String.format("properties文件解析异常 ==> %s", filePath));
            }
            Properties props = new Properties();
            props.load(is);
            Map<String, String> map = new HashMap<String, String>();
            for (Object key : props.keySet()) {
                String value = (String) props.get(key);
                map.put((String) key, value);
            }
            return map;
        } catch (Throwable e) {
            log.error("【配置中心】properties文件解析异常==>{}", filePath, e);
            throw new IllegalArgumentException(String.format("properties文件解析异常 ==> %s", filePath), e);
        } finally {
            IOUtils.closeStream(is);
        }
    }

    public static void main(String[] args) {
        Map<String, String> map = PropertiesUtils.getPropsMap("/project.properties");
        System.out.println(map);
    }

}
